package com.elves.auth.api;

import com.elves.auth.service.MaterialService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;


/**
 * 信息库设备管理API
 */
@Slf4j
@RestController
@RequestMapping("/v1/material")
public class MaterialApi {

    @Resource
    MaterialService materialService;

    // 创建一个方法接收html post请求 上传的文件
    @PostMapping(value = "/import")
    public ModelAndView materialUpload(@RequestParam("file") MultipartFile file, Model model) throws IOException {
        log.info("materialUpload file:{} ", file);
        log.info("materialUpload getName:{}", file.getName());
        log.info("materialUpload length:{}", file.getBytes().length);
        // 从MultipartFile 解析excel文件
        Boolean flag = materialService.importMaterial(file);
        log.info("设备数据导入状态：{}", flag);
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg", "material");
        mav.setViewName("material");
        return mav;
    }


}
